package com.example.home.bus.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.example.home.R;
import com.example.home.bus.bean.OrderBean;

import java.util.List;

public class OrderAdapter extends RecyclerView.Adapter<OrderAdapter.MyHoldy> {
    private Context context;
    private List<OrderBean.RowsBean> list;
    private CallBack callBack;


    public interface CallBack{
        void onItemClick(int position);
    }

    public OrderAdapter(Context context, List<OrderBean.RowsBean> list, CallBack callBack) {
        this.context = context;
        this.list = list;
        this.callBack = callBack;
    }

    @NonNull
    @Override
    public MyHoldy onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.item_order, null);
        return new OrderAdapter.MyHoldy(view);
    }

    @Override
    public void onBindViewHolder(@NonNull MyHoldy holder, int position) {
        holder.path.setText(list.get(position).getPath());
        holder.start.setText(list.get(position).getStart());
        holder.end.setText(list.get(position).getEnd());
        holder.price.setText(String.valueOf(list.get(position).getPrice()));
        holder.orderNum.setText(list.get(position).getOrderNum());
        holder.payTime.setText(String.valueOf(list.get(position).getCreateTime()));
        String statusNum = String.valueOf(list.get(position).getStatus());
        if ("0".equals(statusNum)){
            holder.status.setText("未支付");
        }else if ("1".equals(statusNum)){
            holder.status.setText("已支付");
        }


        if (list.get(position).getStatus() == 0){
            holder.payButton.setVisibility(View.VISIBLE);
            holder.payButton.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (callBack != null) {
                        callBack.onItemClick(position);
                    }
                }
            });
        }else {
            // 订单已支付，隐藏支付按钮
            holder.payButton.setVisibility(View.GONE);
        }
    }

    @Override
    public int getItemCount() {
        return list == null ? 0 : list.size();
    }

    public class MyHoldy extends RecyclerView.ViewHolder {
        private TextView path;
        private TextView start;
        private RelativeLayout rootView;
        private Button payButton;
        private TextView end;
        private TextView price;
        private TextView orderNum;
        private TextView payTime;
        private TextView status;


        public MyHoldy(@NonNull View itemView) {
            super(itemView);

            path = itemView.findViewById(R.id.path);
            rootView = itemView.findViewById(R.id.rootView);
            start = itemView.findViewById(R.id.start);
            end = itemView.findViewById(R.id.end);
            price = itemView.findViewById(R.id.price);
            orderNum = itemView.findViewById(R.id.orderNum);
            payTime = itemView.findViewById(R.id.payTime);
            status = itemView.findViewById(R.id.status);
            payButton = itemView.findViewById(R.id.payButton);

        }
    }
}
